package zxn.circleboard.demo;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.zxn.utils.UIUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ScrollActivity extends AppCompatActivity {

    @BindView(R.id.tv_value)
    TextView tvValue;
    @BindView(R.id.sv_scroll)
    ScrollView svScroll;
    @BindView(R.id.tv_top)
    Button tvTop;
    @BindView(R.id.tv_down)
    Button tvDown;
    @BindView(R.id.tv_show)
    TextView tvShow;
    @BindView(R.id.sb_seekbar)
    SeekBar sbSeekbar;
    @BindView(R.id.rl_seek)
    RelativeLayout rlSeek;
    private String TAG = "ScrollActivity";
    private int screenHeight;
    private int mCurrentProgress;
    private int mRangeH;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll);
        ButterKnife.bind(this);
        UIUtils.init(this);

        tvValue.setText(R.string.text_value);

        sbSeekbar.setMax(100);
        sbSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //Log.i(TAG, "onProgressChanged: --progress:" + progress+"mCurrentProgress:"+mCurrentProgress);
                if (progress > mCurrentProgress) {

                    int value = (int) Math.ceil((progress - mCurrentProgress) / 100f * mRangeH);
                    Log.i(TAG, "onProgressChanged: --progress:" + progress + "mCurrentProgress:" + mCurrentProgress + "value:" + value + "--mRangeH:" + mRangeH);
                    svScroll.scrollBy(0, value);
                    mCurrentProgress = progress;
                } else {//if (progress < mCurrentProgress)
                    int value = (int) Math.ceil((Math.abs(progress - mCurrentProgress)) / 100f * mRangeH);
                    Log.i(TAG, "onProgressChanged: --progress:" + progress + "mCurrentProgress:" + mCurrentProgress + "value:" + value + "--mRangeH:" + mRangeH);
                    svScroll.scrollBy(0, -value);
                    mCurrentProgress = progress;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Log.i(TAG, "onStartTrackingTouch: --progress:" + seekBar.getProgress());
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mCurrentProgress = seekBar.getProgress();
                Log.i(TAG, "onStopTrackingTouch: --progress:" + mCurrentProgress);
            }
        });

        screenHeight = UIUtils.getScreenHeight(this);
        int showH = screenHeight - UIUtils.getDimensionPixelSize(R.dimen.dp_80);
        int totalHeight = svScroll.getChildAt(0).getHeight();
        Log.i(TAG, "onCreate: showH:" + showH + "--screenHeight:" + screenHeight + "--totalHeight:" + totalHeight);
        //showH:2244--screenHeight:2244--totalHeight:0
        svScroll.post(new Runnable() {
            @Override
            public void run() {
                //onCreate: showH:2004--screenHeight:2244--totalHeight:9560--rangeH7316svHeight:1791
                int svHeight = svScroll.getHeight();
                int totalHeight = svScroll.getChildAt(0).getHeight();
                //mRangeH = totalHeight - showH;
                mRangeH = totalHeight - svHeight;
                Log.i(TAG, "onCreate: showH:" + showH + "--screenHeight:"
                        + screenHeight + "--totalHeight:" + totalHeight + "--rangeH" + mRangeH + "svHeight:" + svHeight);
                //onCreate: showH:2004--screenHeight:2244--totalHeight:9560
                //onCreate: showH:2004--screenHeight:2244--totalHeight:9560--rangeH7556
            }
        });
    }

    @OnClick({R.id.tv_top, R.id.tv_down, R.id.tv_show})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_show:
                svScroll.scrollBy(0, mRangeH);
                break;
            case R.id.tv_top:
                svScroll.scrollBy(0, -50);
                break;
            case R.id.tv_down:
                svScroll.scrollBy(0, 50);
                break;
        }
    }


}
